package com.isoft.mapper;

import com.isoft.entity.DfmeBllsEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeBllsMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    void insertMeta(DfmeBllsEntity entity);

    @Insert("INSERT INTO FLIGHT_BASE(" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR})")
    int insertFlightBase(DfmeBllsEntity entity);

    @Insert({
            "<script>",
            "INSERT INTO BAGGAGE_CLAIM(" +
                    "BELT_ID, FLIGHT_ID, BELT_NUMBER, BELT_CODE, BELT_ATTRIBUTE, " +
                    "ESTIMATED_START_TIME, ESTIMATED_END_TIME, REAL_START_TIME, REAL_END_TIME, BELT_SECTION) " +
                    "VALUES ",
            "<foreach collection='dflt.blls.belts' item='belt' separator=','>",
            "(SEQ_BELT.nextval, ",
            "#{dflt.flightId,jdbcType=NUMERIC}, ",
            "#{belt.beltNumber,jdbcType=VARCHAR}, ",
            "#{belt.code,jdbcType=VARCHAR}, ",
            "#{belt.beltAttribute,jdbcType=VARCHAR}, ",
            "TO_TIMESTAMP(#{belt.estimatedStartTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "TO_TIMESTAMP(#{belt.estimatedEndTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "<choose>",
            "<when test='belt.realStartTime != null and belt.realStartTime != \"\"'>",
            "TO_TIMESTAMP(#{belt.realStartTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "</when>",
            "<otherwise>",
            "NULL, ",
            "</otherwise>",
            "</choose>",
            "<choose>",
            "<when test='belt.realEndTime != null and belt.realEndTime != \"\"'>",
            "TO_TIMESTAMP(#{belt.realEndTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), ",
            "</when>",
            "<otherwise>",
            "NULL, ",
            "</otherwise>",
            "</choose>",
            "#{belt.beltSection[0],jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    int insertBaggageClaim(DfmeBllsEntity entity);
}